import axios from 'axios'
import Msg from '@asst/js/msg'

// 配置请求的根路径
// axios.defaults.baseURL = 'http://127.0.0.1:8081/'
// 配置token
axios.interceptors.request.use(config => {
    if (!process.env.MOCK) {
        // 此处为什么要判断mock，是因为这个
        let url = config.url
        let qIndex = url.indexOf('?')

        if (qIndex !== -1) {
            url = url.substring(qIndex)
        }
        if (url.indexOf('ticket') === -1) {
            if (qIndex !== -1) {
                config.url += '&'
            } else {
                config.url += '?'
            }
            // 增加upp-ticket
             config.url += 'ticket=' + sessionStorage.getItem('upp-ticket')
        }

        config.headers.authorization = sessionStorage.getItem('upp-ticket')
    }

    return config
})

export default {
    post (url, data, error, mention = true) {
        // console.log('the req data is====', data)
        return new Promise((resolve, reject) => {
            axios.post(url, data).then(res => {
                let {data, status} = res
                if (status === 200) {
                    if (data.code === '200') {
                        resolve(data)
                    } else if (data.code === '100401') { // 用户未登录
                        // 如果用户登录没有登录，将跳转到单点登录登录页面
                        if (data.data) {
                            let url = data.data
                            let targetIndex = url.indexOf('target')
                            if (targetIndex !== -1) {
                                url = url.substring(0, targetIndex)
                            }
                            const target = encodeURIComponent(location.href)
                            url += 'target=' + target

                            global.open(url, '_self')
                        } else {
                            Msg.alert('未获取到单点登录跳转地址！')
                        }
                    } else {
                        mention && Msg.alert(data.message)
                        reject(data)
                    }
                } else {
                    if (status === 500) {
                        let message = data.message || ''
                        console.log('服务器内部错误！' + message)
                    } else if (status === 530) {
                        mention && Msg.confirm('登录超时，请重新登录！', {
                            onConfirm: () => {
                                window.location.href = '/'
                            }
                        })
                    } else if (status === 501) {
                        mention && Msg.alert('您的请求非法！')
                    } else if (status === 404) {
                        mention && Msg.alert('您请求页面已迷路！')
                    } else {
                        mention && Msg.alert('系统繁忙，请稍后重试！')
                    }
                    reject(data)
                }
                // checkStatus(res)
            }).catch(data => {
                // console.log('error>>>>>>>>>>>>', error)
            })
        })
    },
    request (url, param, okCallback, errCallBack, mention) {
        this.post(url, param, null, mention).then(res => {
            if (res.code === '000000') {
                okCallback && okCallback(res)
            } else {
                errCallBack && errCallBack(res)
            }
        }).catch(err => {
            errCallBack && errCallBack(err)
        })
    }
}
